﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Engine;
using Gencode;
using Library;

namespace GameTool
{
    public partial class CocVang_Form2 : Form
    {
        public delegate void sendPram(string sLop, string sVong);
        public sendPram sender;
        private string _path;
        //public string sPath { get { return _path; } set { _path = value; } }

        public CocVang_Form2(string sPath)
        {
            InitializeComponent();
            _path = sPath;
            InitClass();
        }

        private void InitClass()
        {
            try
            {
                foreach (ClassInfo c in Common.ClassList)
                {

                    if (User.CheckPermission(c.Name)) // Kiem tra quyen duoc xem cua user
                    {
                        TabPage tab = new TabPage();
                        tab.Name = "lop" + c.Name;
                        tab.Text = "Lớp " + c.Name;
                        this.tabClass.TabPages.Add(tab);
                    }
                }
                if (Common.ClassList.Count > 0)
                {
                    tabClass_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        // Ham dung de lay thong tin cua de thi
        private void GetExam(string className)
        {
            try
            {
                // Dua ra danh sach cac vong hien co
                //ArrayList dirList = ftp_main.list_dir(Common.config.pathXml + @"\" + className + @"\Game4");
                ArrayList dirList = ftp_main.list_dir(_path);
                if (dirList == null)
                {
                    return;
                }

                // Lay cac thu muc hien co tren server.
                foreach (string dir in dirList)
                {
                    if (dir.Contains("level"))
                    {
                        string prefix = dir.Substring(5);

                        TreeNode node = tvDeBai.Nodes.Add("Vòng " + prefix);
                        node.Name = dir;

                        ArrayList fileList = ftp_main.list_file(_path + @"\" + dir);

                        if (fileList == null)
                            return;

                        foreach (string file in fileList)
                        {
                            if (file.ToLower() == "1.xml")
                            {
                                int index = file.LastIndexOf('.');
                                prefix = file.Substring(0, index);
                                TreeNode children = node.Nodes.Add("Đề " + prefix);
                                children.Name = prefix + "";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void CocVang_Form2_Load(object sender, EventArgs e)
        {

        }

        private void tabClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                tvDeBai.Nodes.Clear();
                string classname = tabClass.TabPages[tabClass.SelectedIndex].Name;
                GetExam(classname);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void tvDeBai_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                TreeNode selectedNode = tvDeBai.SelectedNode;

                if (!selectedNode.Name.Contains("level"))
                {
                    string sVong = selectedNode.Parent.Name;
                    string sLop = tabClass.SelectedTab.Name;
                    this.sender(sLop, sVong);
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }
    }
}
